﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using LEEAAttendanceControl.AttendanceReference;

using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;

using LEEAAttendanceControl.Web;

namespace LEEAAttendanceControl.Views
{
    public partial class Locations : Page
    {
        public Locations()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.AddEvent += NewLocationHandler;
            myApp.EditEvent += EditLocationHandler;
            myApp.DeleteEvent += DeleteLocationHandler;
        }

        public void NewLocationHandler(object sender, EventArgs e)
        {
            LocationEdit newLocation = new LocationEdit("New Location");
            newLocation.LoadData(0);
            newLocation.Closed += EditClosed;
            newLocation.Show();
        }

        public void EditLocationHandler(object sender, EventArgs e)
        {
            Location selectedLocation = GRDLocation.SelectedItem as Location;

            if (selectedLocation != null)
            {
                LocationEdit editLocation = new LocationEdit("Edit Location");
                editLocation.LoadData(selectedLocation.LocationId);
                editLocation.Closed += EditClosed;
                editLocation.Show();
            }
        }

        public void DeleteLocationHandler(object sender, EventArgs e)
        {
            if (GRDLocation.SelectedItem != null)
            {
                if (MessageBox.Show(ApplicationStrings.DeleteMessage, ApplicationStrings.Delete
                    , MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    LocationsDataSource.DataView.Remove(GRDLocation.SelectedItem);

                    LocationsDataSource.SubmitChanges();
                }
            }
        }

        protected void EditClosed(object sender, EventArgs e)
        {
            LocationsDataSource.Load();
        }

        private void LocationsDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }
    }
}
